package com.keyue.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.keyue.common.constant.Constant;
import com.keyue.common.constant.ServerResult;
import com.keyue.domain.dto.SysLogDO;
import com.keyue.domain.vo.SysLogQueryVO;
import com.keyue.domain.vo.SysLogVO;
import com.keyue.mapper.SysLogMapper;
import com.keyue.service.SysLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 系统日志(SysLog)表服务实现类
 *
 * @author makejava
 * @since 2023-09-06 16:24:59
 */
@Slf4j
@Service
public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLogDO> implements SysLogService {


    /**
     * 通过ID查询单条数据
     * <p>
     * sysLogQueryVO 主键
     *
     * @return 实例对象
     */
    @Override
    public ServerResult queryById(SysLogQueryVO sysLogQueryVO) {
        if (Objects.isNull(sysLogQueryVO.getId())) {
            return ServerResult.fail("id 不能为空");
        }
        SysLogDO SysLogDo = getById(sysLogQueryVO.getId());
        if (Objects.isNull(SysLogDo)) {
            return ServerResult.success(null);
        }
        return ServerResult.success(BeanUtil.copyProperties(SysLogDo, SysLogVO.class));
    }

    /**
     * 分页查询
     *
     * @param sysLogQueryVO 筛选条件
     * @return 查询结果
     */
    @Override
    public ServerResult list(SysLogQueryVO sysLogQueryVO) {
        return ServerResult.success();
    }

    /**
     * 新增数据
     *
     * @param sysLogVO 实例对象
     * @return 实例对象
     */
    @Override
    public ServerResult add(SysLogVO sysLogVO) {
        //todo 校验
        if (Objects.nonNull(sysLogVO.getId())) {
            sysLogVO.setId(null);
        }
        SysLogDO sysLogDo = BeanUtil.copyProperties(sysLogVO, SysLogDO.class);

        save(sysLogDo);

        return ServerResult.success();
    }

    /**
     * 修改数据
     *
     * @param sysLogVO 实例对象
     * @return 实例对象
     */
    @Override
    public ServerResult edit(SysLogVO sysLogVO) {
        //todo 校验
        if (Objects.isNull(sysLogVO.getId())) {
            return ServerResult.fail("id 不能为空");
        }
        SysLogDO sysLogDo = BeanUtil.copyProperties(sysLogVO, SysLogDO.class);

        updateById(sysLogDo);

        return ServerResult.success();
    }

    /**
     * 通过主键删除数据
     *
     * @param sysLogQueryVO 筛选条件
     * @return 是否成功
     */
    @Override
    public ServerResult deleteById(SysLogQueryVO sysLogQueryVO) {
        List<Long> ids = sysLogQueryVO.getIds();
        if (CollUtil.isEmpty(ids)) {
            return ServerResult.fail("ids 参数不能为空");
        }
        List<SysLogDO> sysLogList = listByIds(ids);
        if (CollUtil.isEmpty(sysLogList)) {
            return ServerResult.success();
        }
        //todo 校验是否可以逻辑删除
        LambdaUpdateWrapper<SysLogDO> updateWrapper = new LambdaUpdateWrapper<SysLogDO>()
                .in(SysLogDO::getId, ids)
                .set(SysLogDO::getEnableFlag, Constant.UNABLE_TWENTY);
        update(updateWrapper);
        return ServerResult.success();
    }
}
